package org.komarichin.jdo.dao;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import org.komarichin.beans.IComment;
import org.komarichin.beans.IGenericEntity;
import org.komarichin.beans.IHasComment;
import org.komarichin.beans.dao.ICommentDAO;
import org.komarichin.jdo.persistence.Comment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * &nbsp;
 * </p>
 * User: Michael Komarichin
 * Date: Mar 7, 2010
 * Time: 9:48:23 PM
 */
@Transactional(readOnly = true)
public class CommentDAO extends JdoGenericDAOSupport<Comment, String> implements ICommentDAO<Comment, String> {
    private static final Logger log = LoggerFactory.getLogger(CommentDAO.class);
    private static final long serialVersionUID = -2425601301231047744L;

    protected CommentDAO(Class<Comment> type) {
        super(type);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(readOnly = false)
    public String saveOrUpdate(Comment entity) {
        log.info("save or update entity: {}", entity);
        Comment c = getJdoTemplate().getObjectById(getType(), entity.getId());
        if (c == null) {
            return save(entity).getId();
        } else {
            c.setContent(entity.getContent());
            c.setEmail(entity.getEmail());
            c.setUser(entity.getUser());
            return c.getId();
        }
    }

    @Override
    public Collection<IComment<String>> getCommentsForObject(Class<? extends IHasComment<IComment<String>, IGenericEntity>> classObj, Object idValue) {
        log.info("getCommentForObject#obj class: {}, id value: {}", classObj);
        IHasComment<IComment<String>, IGenericEntity> obj = getJdoTemplate().getObjectById(classObj, idValue);
        if (obj == null) {
            log.warn("object not found by id: {}", idValue);
            return null;
        }
        List<IComment<String>> comments = obj.getComments();
        comments.size();
        log.debug("result size: {}", comments.size());
        obj.setComments(comments);
        return comments;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void assignCommentToObject(IHasComment<IComment<String>, IGenericEntity> obj, IComment<String> comment) {
        IHasComment<IComment, String> com = getJdoTemplate().getObjectById(obj.getClass(), obj.getId());
        Key key = new KeyFactory.Builder(obj.getClass().getName(), String.valueOf(com.getId())).addChild(Comment.class.getName(), String.valueOf(comment.hashCode())).getKey();
        comment.setId(KeyFactory.keyToString(key));
        com.getComments().add(comment);
    }

    @Override
    @SuppressWarnings("unchecked")
    public Collection<IComment<String>> getCommentsForObject(IHasComment<IComment<String>, IGenericEntity> obj) {
        log.info("getCommentsForObject: {}", obj);
        obj = getJdoTemplate().getObjectById(obj.getClass(), obj.getId());
        if (obj == null) {
            log.error("object is null");
            return null;
        }
        List<IComment<String>> comments = obj.getComments();
        comments.size();
        log.debug("result size: {}", comments.size());
        obj.setComments(comments);
        return comments;
    }

    @Override
    public Comment newInstance() {
        return new Comment();
    }
}
